package org.csu.mall.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

import java.io.Serializable;

@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UniformResponse<T> implements Serializable {

    private int status;

    private String msg;

    private T data;

    private UniformResponse(int status){
        this.status = status;
    }

    private UniformResponse(int status, String msg){
        this.status = status;
        this.msg = msg;
    }

    private UniformResponse(int status, String msg, T data){
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    private UniformResponse(int status, T data){
        this.status = status;
        this.data = data;
    }

    @JsonIgnore
    public boolean isSuccess(){
        return this.getStatus() == ResponseCode.SUCCESS.getCode();
    }

    public static <T> UniformResponse<T> createForSuccess(){
        return new UniformResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> UniformResponse<T> createForSuccessMessage(String msg){
        return new UniformResponse<T>(ResponseCode.SUCCESS.getCode(), msg);
    }

    public static <T> UniformResponse<T> createForSuccess(T data){
        return new UniformResponse<T>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> UniformResponse<T> createForSuccess(String msg, T data){
        return new UniformResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> UniformResponse<T> createForError(){
        return new UniformResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDescription());
    }

    public static <T> UniformResponse<T> createForErrorMessage(String msg){
        return new UniformResponse<T>(ResponseCode.ERROR.getCode(), msg);
    }

    public static <T> UniformResponse<T> createForErrorCodeMessage(int code, String msg){
        return new UniformResponse<T>(code, msg);
    }
}
